
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { getStatusUpDate,getUpdate} from './templatesListServ.js'
import { makeAddress } from "utils/util"

const defultState = {
    biz:{},
    saveData:{},
    modalObj:{
      visible:false
    },
    hashUrl:''
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
          dispatch({type: 'updateStore', payload: {hashUrl:pathname}})
        }
      })
    },
  },


  effects: {
      //更新状态与删除
      *updateStatus({ payload }, { put, call, select }) {
        
          try{
             console.log(payload,'payload')
             const { rows, success,total,pageNo } = yield call(getStatusUpDate,payload);
             let defultState = yield select(state => state[tmpModule.namespace])
             if(success){
                message.success('删除成功',()=>{
                  defultState.biz.refreshListDatas(true)
                })
             }
          }catch(e){
              console.error(e)
          }
      },
     
     //更新状态与删除
     *update({ payload }, { put, call, select }) {
      try{
         console.log(payload,'payload')
         const { rows, success,total,pageNo } = yield call(getUpdate,payload);
         let defultState = yield select(state => state[tmpModule.namespace])
         if(success){
            message.success('更新成功',()=>{
              defultState.biz.refreshListDatas(true)
            })
         }
      }catch(e){
          console.error(e)
      }
  },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
